package activity.projeto;


import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint({ "ShowToast", "ShowToast" })
public class Login extends Activity{
	
	EditText etNick, etSenha;
	Button btAcessar, btCadastrar, btEsqueci, btOk, btOff;
	
	MediaPlayer mp = new MediaPlayer(); //objeto do audio

    
public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.main);
		
		etNick = (EditText) findViewById(R.id.nick);
		etSenha = (EditText) findViewById(R.id.senha);
		btAcessar = (Button) findViewById(R.id.entrar);
		btCadastrar = (Button) findViewById(R.id.cadastra);
	    btEsqueci = (Button) findViewById(R.id.esqueci);
	    btOff = (Button) findViewById(R.id.off);
		
		btAcessar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(etNick.getText() != null && etSenha.getText() !=null){
				
				// TODO Auto-generated method stub
				Log.i("Login", " Entrou no evento");
				// URL com endreço da aplicação
				String urlPost = "http://192.168.2.2/login/login.php";
				// URL GET
				String urlGet = "http://10.65.102.16/login/login.php?nick="+etNick.getText().toString()+"&senha="+etSenha.getText().toString();
				// Parametros que serão passados através do método Post
				ArrayList<NameValuePair> paramPost = new ArrayList<NameValuePair>();
				paramPost.add(new BasicNameValuePair("nick", etNick.getText().toString()));
				paramPost.add(new BasicNameValuePair("senha", etSenha.getText().toString()));
				// String de resposta
				String responseReturned = null;
				Log.i("Login", " Vai entrar no Try");
				// Try-Catch
				try {
					responseReturned = DataBaseHttpConnection.executeHttpGET(urlGet);
					String response = responseReturned.toString();
					Log.i("Login", " Resposta = " +response);
					response = response.replaceAll("\\s+", "");
					if (response.equals("1")){
						Intent telaPers = new Intent(Login.this,Personagens.class);
						Login.this.startActivity(telaPers);    		
						Login.this.finish();
						showMessage(" Login " , " Usuário Válido ");					
					}
		    		else
		    			showMessage(" Login " , " Usuário Inválido ");
				} catch (Exception e) {
					// TODO: handle exception
					Toast.makeText(Login.this, "Erro.: " +e, Toast.LENGTH_LONG);
				}
				
				}//condi��o	
				else
					showMessage(" Login " , " Algum dos campos est� em vazio ");
					
				
			}
		});
		
		btCadastrar.setOnClickListener(new View.OnClickListener(){//evento do botão de cadastrar
	    	public void onClick(View arg0) {
	    		Intent telacad = new Intent(Login.this,Cadastrar.class);
	    		Login.this.startActivity(telacad);    		
	    		Login.this.finish();
	    		
	    	}
	    	
	    });
				
		
		
		btOff.setOnClickListener(new View.OnClickListener(){//evento do botão de jogar
	    	public void onClick(View arg0) {
	    		Intent telajogo = new Intent(Login.this,Personagens.class);
	    		Login.this.startActivity(telajogo);
	    		Login.this.finish();
	    		
	    	}
	    	
	    });
		
		
		
		
		 Context  context = Login.this; //especifique o context ou obtenha usando um getBaseContext(); 

         mp.release(); //prepara activity
         mp = MediaPlayer.create(context , R.raw.trilhajogo); //cria
         mp.start();//executa
        
}//Fim do OnCreate

	public void onBackPressed() {  //botão voltar
		
		Login.this.finish(); 		
		mp.stop();
	    super.onBackPressed(); // OK - botão back funciona 
	   return;
	}
	
	public void showMessage(String title, String text){
		AlertDialog.Builder message = new AlertDialog.Builder(Login.this);
		message.setTitle(title);
		message.setMessage(text);
		message.setNeutralButton("OK", null);
		message.show();				
	}
	

}
